package me.stone.stanimeclient.util

import android.graphics.Color
import android.graphics.drawable.ColorDrawable
import android.graphics.drawable.Drawable
import android.os.Build
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import androidx.appcompat.content.res.AppCompatResources
import com.bumptech.glide.Glide
import com.bumptech.glide.load.DataSource
import com.bumptech.glide.load.engine.GlideException
import com.bumptech.glide.request.RequestListener
import com.bumptech.glide.request.target.Target
import eightbitlab.com.blurview.BlurAlgorithm
import eightbitlab.com.blurview.BlurView
import eightbitlab.com.blurview.RenderEffectBlur
import eightbitlab.com.blurview.RenderScriptBlur
import me.stone.stanimeclient.R

abstract class DefaultDrawableListener: RequestListener<Drawable> {
    override fun onLoadFailed(
        e: GlideException?,
        model: Any?,
        target: Target<Drawable>,
        isFirstResource: Boolean
    ): Boolean {
        return false
    }

    override fun onResourceReady(
        resource: Drawable,
        model: Any,
        target: Target<Drawable>?,
        dataSource: DataSource,
        isFirstResource: Boolean
    ): Boolean {
        onDrawableGet(resource)
        return false
    }

    abstract fun onDrawableGet(drawable: Drawable)
}

fun ImageView.imageFromUrl(url: String) {
    Glide.with(this)
        .load(url)
        .error(AppCompatResources.getDrawable(context, R.mipmap.empty_cover))
        .into(this)
}

fun BlurView.setupBlur(drawable: Drawable, radius: Float = 30f) {
    val algorithm: BlurAlgorithm =
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
            RenderEffectBlur()
        } else {
            RenderScriptBlur(context)
        }
    val parentGroup = parent as ViewGroup
    parentGroup.background = drawable
    setupWith(parentGroup, algorithm)
        .setFrameClearDrawable(drawable)
        .setOverlayColor(Color.argb(0x33, 0x00, 0x00, 0x00))
        .setBlurRadius(radius)
}

//private val appExecutors = AppExecutors()

fun View.loadBackgroundFromUrl(url: String, commit: ((Drawable?) -> Unit)? = null) {
    background = ColorDrawable(Color.DKGRAY)
//    appExecutors.networkIO().execute {
//        val drawable = try {
//            Glide.with(this).load(url).submit().get()
//        } catch (e: Exception) {
//            debugPrint(e.toString())
//            null
//        }
//        appExecutors.mainThread().execute {
//            drawable?.let { background = it }
//            commit?.invoke(drawable)
//        }
//    }
}

fun View.setBlurBackground(drawable: Drawable, blurRadius: Float = 30f) {
    val algorithm: BlurAlgorithm =
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
            RenderEffectBlur()
        } else {
            RenderScriptBlur(context)
        }
    post {
//        appExecutors.diskIO().execute {
//            debugPrint("view -> width: $width, height: $height")
//            val originBitmap = drawable.toBitmap(width, height)
//            val blurBitmap = algorithm.blur(originBitmap, blurRadius)
//            val blurDrawable = BitmapDrawable(resources, blurBitmap)
//            appExecutors.mainThread().execute {
//                debugPrint("calculate blur drawable -> $blurDrawable")
//                background = blurDrawable
//            }
//        }
    }
}